package edu.hnu.cloudflight.android.util;

import java.util.ArrayList;
import java.util.List;

import edu.hnu.cloudflight.android.MyApplication;
import edu.hnu.cloudflight.android.R;
import edu.hnu.cloudflight.android.domain.Flight;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class StringUtil {
	
	public static String converterToFirstSpell(String chines) {
		String pinyinName = "";
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName += nameChar[i];
			}
		}
		return pinyinName;
	}

	public static String converterToAllSpell(String chines) {
		String pinyinName = "";
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName += nameChar[i];
			}
		}
		return pinyinName;
	}
	
	public static String getStringFromRes(int id){
		return MyApplication.res.getString(id);
	}
	
	
	public static boolean isEmpty(String s){
		if(s==null||"".equals(s)){
			return true;
		}
		return false;
	}
	
	
	public static List<String> getStringFromFlight(Flight f){
		if(f==null){
			return null;
		}
		List<String> list = new ArrayList<String>();
		list.add(getStringFromRes(R.string.flight_name)+f.name);
		list.add(getStringFromRes(R.string.flight_complany)+f.complany);
		list.add(getStringFromRes(R.string.flight_airmodel)+f.AirModel);
		list.add(getStringFromRes(R.string.flight_airage)+f.AirAge+getStringFromRes(R.string.time_year));
		list.add(getStringFromRes(R.string.flight_start)+f.start);
		list.add(getStringFromRes(R.string.flight_end)+f.end);
		list.add(getStringFromRes(R.string.flight_ontimerate)+f.OnTimeRate);
		list.add(getStringFromRes(R.string.flight_status)+f.status);
		list.add(getStringFromRes(R.string.flight_deptime)+f.DepTime);
		list.add(getStringFromRes(R.string.flight_arrtime)+f.ArrTime);
		list.add(getStringFromRes(R.string.flight_dactual)+f.Dactual);
		list.add(getStringFromRes(R.string.flight_aactual)+f.Aactual);
		list.add(getStringFromRes(R.string.flight_dexpected)+f.Dexpected);
		list.add(getStringFromRes(R.string.flight_aexpected)+f.Aexpected);
		list.add(getStringFromRes(R.string.flight_food)+("1".equals(f.food) ?"有":"无"));
		list.add(getStringFromRes(R.string.flight_flytime)+f.FlyTime+getStringFromRes(R.string.time_min));
		return list;
	}
}
